package com.hmj.util;

import com.hmj.javabean.User;
import mapper.UserMapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.util.ArrayList;
import java.util.List;

public class UserMapperTest {
    SqlSession session;
    UserMapper mapper;

    @Before
    public void before() {
        SqlSessionFactory factory = MyBatisUtil.getSqlSessionFactory();
        session = factory.openSession(true);
        mapper = session.getMapper(UserMapper.class);

    }

    @After
    public void after() {
        //5、关闭资源
        session.close();
    }

    @Test
    public void selectAll() {
        for (User user : mapper.selectAll()) {
            System.out.println(user);
        }
    }

    @Test
    public void trimTest(){
        mapper.trimTest();
    }

    @Test
    public void selectByUser() {
        User u = new User();
        u.setUid(1);
        for (User user : mapper.selectByUser(u)) {
            System.out.println(user);
        }
    }

    @Test
    public void selectByUserOne() {
        User u = new User();

        for (User user : mapper.selectByUserOne(u)) {
            System.out.println(user);
        }
    }


    @Test
    public void updateByUser() {
        User u = new User();
        u.setUid(1);
        u.setUsername("你好");
        u.setPassword("5623789");
        mapper.updateByUser(u);
    }

    @Test
    public void deleteByArray() {
        int[] uid = {1, 2, 3, 4, 5, 6};
        mapper.deleteByArray(uid);
    }

    @Test
    public void insertList() {
        List<User> list = new ArrayList<>();
        list.add(new User("1", "2", "3"));
        list.add(new User("b", "b", "b"));
        list.add(new User("c", "c", "c"));
        mapper.insertList(list);
    }
}
